import React, { Component } from 'react'
import './index.css'
export default class Item extends Component {
  
  state = {mouse:false}
  
  handleMouse = (flag)=>{
    return ()=>{
      
      this.setState({
        mouse:flag
      })
      //console.log('state',this.state)
    }
    
  }
  handleDelete = (id)=>{
    
      return ()=>{
        if(window.confirm('delete?')){
          //console.log(id)
          this.props.deleteTodo(id)
        }
        
      }
  
    
  }
  handleCheck = (id)=>{
    return (event)=>{
      //console.log(id,event.target.checked)
      this.props.updateTodo(id,event.target.checked)
    }
    
  }

  render() {
    const {todo} = this.props
    //console.log(todo.done)
    const {mouse} = this.state
    const flag = todo.done
    console.log("item==>",todo.id,todo.done);
    return (
      <li style={{backgroundColor:mouse?'#ddd':'white'}} onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)}>
        <label>
          <input type="checkbox"  onChange={this.handleCheck(todo.id)} checked={flag}/>
          <span>{todo.name}</span>
        </label>
        <button onClick={this.handleDelete(todo.id)} className="btn btn-danger" style={{display:mouse?'block':'none'}}>删除</button>
      </li>
    )
  }
}
